#include<iostream>
#include<opencv2/imgcodecs.hpp>
#include<opencv2/highgui.hpp>
#include<opencv2/imgproc.hpp>
#include<opencv2/objdetect.hpp>
#include<opencv2/opencv.hpp>
#include<opencv2/ximgproc.hpp>
#include<string>
#include<vector>
#include<fstream>
#include <fcntl.h>
#include <termios.h>
#include"func.h"

using namespace std;
using namespace cv;

void facerecognize_init(CascadeClassifier& faceCasade)//人脸识别初始化函数加载人脸识别模型
{
    // CascadeClassifier faceCasade;
    faceCasade.load("/home/zhanjiabin/桌面/task/Gimbal/haarcascade_frontalface_default.xml");

    if(faceCasade.empty())
    {
        cout<<"XML file not load"<<endl;
    }
}

void facerecognize(Mat img,vector<Rect>& faces,CascadeClassifier& faceCasade,Result& result)//人脸识别函数
{
    faceCasade.detectMultiScale(img,faces,1.1,10);
    if(faces.empty())
    {
        result.targetX=0;
        result.targetY=0;
        cout<<"no target"<<endl;
        cout << "target: (" << result.targetX<< ", " << result.targetY<< ")" << endl;
    }
        for(int i=0;i<faces.size();i++)
        {
            // Mat imgCrop=img(faces[i]);
            // imshow(to_string(i),imgCrop);
            //imwrite("/home/zhanjiabin/桌面/task/face/build"+to_string(i)+".jpg",imgCrop);
            rectangle(img,faces[i].tl(),faces[i].br(),Scalar(255,0,255),3);
            result.targetX=(faces[i].x+faces[i].width/2);
            result.targetY=(faces[i].y+faces[i].height/2);
            cout << "target: (" << result.targetX << ", " << result.targetY << ")" << endl;
        }

        imshow("Image",img);
}
